<script setup lang='ts'>
import type { Ref } from 'vue'
import { computed, onMounted, onUnmounted, ref, defineAsyncComponent} from 'vue'
import { useRoute } from 'vue-router'
import { storeToRefs } from 'pinia'
import { NAutoComplete, NButton, NInput, useDialog, useMessage, NSelect, NPopover,NUpload, UploadFileInfo} from 'naive-ui'
import { Message } from './components'
import { useScroll } from './hooks/useScroll'
import { useChat } from './hooks/useChat'
import { useUsingContext } from './hooks/useUsingContext'
import HeaderComponent from './components/Header/index.vue'
import { HoverButton, SvgIcon } from '@/components/common'
import { useBasicLayout } from '@/hooks/useBasicLayout'
import { useChatStore, usePromptStore } from '@/store'
import { fetchChatAPIProcess,saveMsg } from '@/api'
import { t } from '@/locales'
import { serviceTypeOptions } from '@/api/ServiceTypeConstant'

let controller = new AbortController()
const Setting = defineAsyncComponent(() => import('@/components/common/Setting/index.vue'))
const route = useRoute()
const dialog = useDialog()
const chatStore = useChatStore()
const { isMobile } = useBasicLayout()
const { addChat, updateChat, updateChatSome, getChatByUuidAndIndex } = useChat()
const { scrollRef, scrollToBottom, scrollToBottomIfAtBottom } = useScroll()
const { usingContext, toggleUsingContext } = useUsingContext()
const { uuid } = route.params as { uuid: string }
const dataSources = computed(() => chatStore.getChatByUuid(uuid))
const prompt = ref<string>('')
const loading = ref<boolean>(false)
const imgurl = ref<string>('')
const inputRef = ref<Ref | null>(null)
// 添加PromptStore
const promptStore = usePromptStore()
const message = useMessage()
// 使用storeToRefs，保证store修改后，联想部分能够重新渲染
const { promptList: promptTemplate } = storeToRefs<any>(promptStore)

// 未知原因刷新页面，loading 状态不会重置，手动重置
dataSources.value.forEach((item, index) => {
  if (item.loading)
    updateChatSome(uuid, index, { loading: false })
})
function handleSubmit() {
  onConversation()
}

async function onConversation() {
  let message = prompt.value
  // await requestCheckContent(message) // 敏感词校验
  if (loading.value)
    return

  if (!message || message.trim() === '')
    return

  controller = new AbortController()
  addChat(
    uuid,
    {
      dateTime: new Date().toLocaleString(),
      text: message,
      inversion: true,
      error: false,
      conversationOptions: null,
      requestOptions: { prompt: message, options: null },
    },
  )
  scrollToBottom()

  loading.value = true
  prompt.value = ''
  let options: Chat.ConversationRequest = {}
  // 添加用户回话信息
  addChat(
    uuid,
    {
      dateTime: new Date().toLocaleString(),
      text: '',
      loading: true,
      inversion: false,
      error: false,
      conversationOptions: null,
      requestOptions: { prompt: message, options: { ...options } },
    },
  )
  // 滚动屏幕到最低端
  scrollToBottom()
  let lastText = ''
  try {
    const fetchChatAPIOnce = async () => {
      await fetchChatAPIProcess<Chat.ConversationResponse>({
        serviceType: serviceType.value,
        usingContext:usingContext.value,
        conversationId: uuid,
        prompt: message,
        imgurl: imgurl.value,
        options,
        signal: controller.signal,
        onDownloadProgress: ({ event }) => {
          const xhr = event.target
          const { responseText } = xhr
          let chunk
          if(imgurl.value && serviceType.value=="gpt-4-vision-preview"){
             chunk = '<img src='+imgurl.value+' width=160 height=160/>'+responseText
          }else{
            chunk = responseText
          }
          try {
            const data = chunk
            updateChat(
              uuid,
              dataSources.value.length - 1,
              {
                dateTime: new Date().toLocaleString(),
                text: data ?? '',
                inversion: false,
                error: false,
                loading: true,
                conversationOptions: { conversationId: '', parentMessageId: '' },
                requestOptions: { prompt: message, options: { ...options } },
              },
            )
            lastText = data;
            scrollToBottomIfAtBottom()
          }
          catch (error) {
            console.log(error)
          }
        },
      })
      updateChatSome(uuid, dataSources.value.length - 1, { loading: false })
      scrollToBottom()
    }

    await fetchChatAPIOnce()
  } catch (error: any) {
    const errorMessage = error?.message ?? t('common.wrong')

    if (error.message === 'canceled') {
      updateChatSome(
        uuid,
        dataSources.value.length - 1,
        {
          loading: false,
        },
      )
      scrollToBottomIfAtBottom()
      return
    }

    const currentChat = getChatByUuidAndIndex(uuid, dataSources.value.length - 1)

    if (currentChat?.text && currentChat.text !== '') {
      updateChatSome(
        uuid,
        dataSources.value.length - 1,
        {
          text: `${currentChat.text}\n[${errorMessage}]`,
          error: false,
          loading: false,
        },
      )
      return
    }

    updateChat(
      uuid,
      dataSources.value.length - 1,
      {
        dateTime: new Date().toLocaleString(),
        text: errorMessage,
        inversion: false,
        error: true,
        loading: false,
        conversationOptions: null,
        requestOptions: { prompt: message, options: { ...options } },
      },
    )
    scrollToBottomIfAtBottom()
  }
  finally {
    loading.value = false
    // 保存消息记录
    saveMsg(lastText,serviceType.value)
  }
}

async function onRegenerate(index: number) {
  if (loading.value)
    return

  controller = new AbortController()

  const { requestOptions } = dataSources.value[index]

  let message = requestOptions?.prompt ?? ''

  let options: Chat.ConversationRequest = {}

  if (requestOptions.options)
    options = { ...requestOptions.options }

  loading.value = true

  updateChat(
    uuid,
    index,
    {
      dateTime: new Date().toLocaleString(),
      text: '',
      inversion: false,
      error: false,
      loading: true,
      conversationOptions: null,
      requestOptions: { prompt: message, options: { ...options } },
    },
  )

  try {
    const fetchChatAPIOnce = async () => {
      await fetchChatAPIProcess<Chat.ConversationResponse>({
        serviceType: serviceType.value,
        usingContext: usingContext.value,
        conversationId: uuid,
        prompt: message,
        imgurl: imgurl.value,
        options,
        signal: controller.signal,
        onDownloadProgress: ({ event }) => {
          const xhr = event.target
          const { responseText } = xhr
          let chunk = responseText
          try {
            const data = chunk
            updateChat(
              uuid,
              index,
              {
                dateTime: new Date().toLocaleString(),
                text:data ?? '',
                inversion: false,
                error: false,
                loading: true,
                conversationOptions: { conversationId: data.conversationId, parentMessageId: data.id },
                requestOptions: { prompt: message, options: { ...options } },
              },
            )
          }
          catch (error) {
          }
        },
      })
      updateChatSome(uuid, index, { loading: false })
    }
    await fetchChatAPIOnce()
  }
  catch (error: any) {
    if (error.message === 'canceled') {
      updateChatSome(
        uuid,
        index,
        {
          loading: false,
        },
      )
      return
    }

    const errorMessage = error?.message ?? t('common.wrong')

    updateChat(
      uuid,
      index,
      {
        dateTime: new Date().toLocaleString(),
        text: errorMessage,
        inversion: false,
        error: true,
        loading: false,
        conversationOptions: null,
        requestOptions: { prompt: message, options: { ...options } },
      },
    )
  }
  finally {
    loading.value = false
  }
}

function handleDelete(index: number) {
  if (loading.value)
    return

  dialog.warning({
    title: t('chat.deleteMessage'),
    content: t('chat.deleteMessageConfirm'),
    positiveText: t('common.yes'),
    negativeText: t('common.no'),
    onPositiveClick: () => {
      chatStore.deleteChatByUuid(uuid, index)
    },
  })
}

function handleClear() {
  if (loading.value)
    return

  dialog.warning({
    title: t('chat.clearChat'),
    content: t('chat.clearChatConfirm'),
    positiveText: t('common.yes'),
    negativeText: t('common.no'),
    onPositiveClick: () => {
      chatStore.clearChatByUuid(uuid)
    },
  })
}

function handleEnter(event: KeyboardEvent) {
  if (!isMobile.value) {
    if (event.key === 'Enter' && !event.shiftKey) {
      event.preventDefault()
      handleSubmit()
    }
  }
  else {
    if (event.key === 'Enter' && event.ctrlKey) {
      event.preventDefault()
      handleSubmit()
    }
  }
}

function handleStop() {
  if (loading.value) {
    controller.abort()
    loading.value = false
  }
}

function beforeUpload(data: {
  file: UploadFileInfo
  fileList: UploadFileInfo[]
}) {
  if (data.file.file?.type !== 'image/png') {
    message.error('只能上传png格式的图片文件,请重新上传')
    return false
  }
  return true
}

function handleFinish({
      event
    }: {
      file: UploadFileInfo
      event?: ProgressEvent
    }) {
      const ext = (event?.target as XMLHttpRequest).response
      let json = JSON.parse(ext)
      if (json.code === 200) {
        imgurl.value = json.data.url;
      }
      message.success('上传成功！')

}

// 可优化部分
// 搜索选项计算，这里使用value作为索引项，所以当出现重复value时渲染异常(多项同时出现选中效果)
// 理想状态下其实应该是key作为索引项,但官方的renderOption会出现问题，所以就需要value反renderLabel实现
const searchOptions = computed(() => {
  // if (prompt.value.startsWith('/')) {
  //   return promptTemplate.value.filter((item: { key: string }) => item.key.toLowerCase().includes(prompt.value.substring(1).toLowerCase())).map((obj: { value: any }) => {
  //     return {
  //       label: obj.value,
  //       value: obj.value,
  //     }
  //   })
  // }
  // else {
  //   return []
  // }
  return promptTemplate.value.filter((item: { key: string }) => item.key.toLowerCase().includes(prompt.value.substring(1).toLowerCase())).map((obj: { value: any }) => {
    return {
      label: obj.value,
      value: obj.value,
    }
  })
})

// value反渲染key
const renderOption = (option: { label: string }) => {
  for (const i of promptTemplate.value) {
    if (i.value === option.label)
      return [i.key]
  }
  return []
}

const placeholder = computed(() => {
  if (isMobile.value)
    return t('chat.placeholderMobile')
  return t('chat.placeholder')
})

const buttonDisabled = computed(() => {
  return loading.value || !prompt.value || prompt.value.trim() === ''
})


const footerClass = computed(() => {
  let classes = ['p-4']
  if (isMobile.value)
    classes = ['sticky', 'left-0', 'bottom-5', 'right-0', 'p-2', 'pr-3', 'overflow-hidden']
  return classes
})

onMounted(() => {
  scrollToBottom()
  if (inputRef.value && !isMobile.value)
    inputRef.value?.focus()
})

onUnmounted(() => {
  if (loading.value)
    controller.abort()
})

const showUs = ref(false)

const serviceType = ref("gpt-3.5-turbo-0613")

// async function getMjImage(taskId: any) {
// 		let taskResult = await requestGetImgByTaskId(taskId);
//     return taskResult; // 返回获取到的图片地址
// }

</script>

<template>
  <div class="flex flex-col w-full h-full">
    <HeaderComponent v-if="isMobile" :using-context="usingContext" @export="handleExport"
      @toggle-using-context="toggleUsingContext" />
    <main class="flex-1 overflow-hidden">
      <div id="scrollRef" ref="scrollRef" class="h-full overflow-hidden overflow-y-auto">
        <div v-if="false" style="width: 100%;" class="flex items-center justify-center mt-4 text-center text-neutral-300">
          <span v-if="isMobile" style="width: 20%"></span>
          <span v-if="!isMobile" style="width: 30%"></span>
          <div style="width: 100%;" class="flex items-center justify-center ml-4">
            <span style="color: #999999"><b>模型</b></span>
            <NSelect style="width: 240px" v-model:value="serviceType" :key="serviceType" :options="serviceTypeOptions" />
          </div>
          <span v-if="!isMobile" style="width: 30%"></span>
          <span style="width: 30%;color: #999999" class="flex-shrink-0 w-[100px]"
            @click="showUs = true"><b>联系我们</b></span>
        </div>
        <!--				<PromptModel v-if="!dataSources.length" :visible="true" />-->
        <div id="image-wrapper" class="w-full max-w-screen-xl m-auto dark:bg-[#101014]"
          :class="[isMobile ? 'p-2' : 'p-4']">
          <!--					<div style="position: fixed; width: 60%;height: 20px">-->
          <template v-if="dataSources.length">
            <div>
              <Message v-for="(item, index) of dataSources" :key="index" :date-time="item.dateTime" :text="item.text"
                :inversion="item.inversion" :error="item.error" :loading="item.loading" @regenerate="onRegenerate(index)"
                @delete="handleDelete(index)" 
                :image-url="imgurl"
                />
              <div class="sticky bottom-0 left-0 flex justify-center">
                <NButton v-if="loading" type="warning" @click="handleStop">
                  <template #icon>
                    <SvgIcon icon="ri:stop-circle-line" />
                  </template>
                    停止
                </NButton>
              </div>
            </div>
          </template>
        </div>
      </div>
    </main>
    <footer :class="footerClass">
      <div class="w-full max-w-screen-xl m-auto">
        <NSelect v-if="isMobile" style="width: 175px" v-model:value="serviceType" :key="serviceType"
          :options="serviceTypeOptions" />
        <div class="flex items-center justify-between space-x-2">
          <NSelect v-if="!isMobile" style="width: 230px" v-model:value="serviceType" :key="serviceType"
            :options="serviceTypeOptions" />
          <HoverButton @click="handleClear">
            <span class="text-xl text-[#4f555e] dark:text-white">
              <SvgIcon icon="ri:delete-bin-line" />
            </span>
          </HoverButton>
          <!--					是否带上聊天记录-->
          <n-popover trigger="hover">
            <template #trigger>
              <HoverButton @click="toggleUsingContext">
                <span   class="text-xl" :class="{ 'text-[#4b9e5f]': usingContext, 'text-[#a8071a]': !usingContext }">
                  <SvgIcon icon="ri:chat-history-line" />
                </span>
              </HoverButton>
            </template>
            是否打开聊天上下文
          </n-popover>
          
          <HoverButton >
              <n-upload
              action="/api/resource/oss/upload"
              @finish="handleFinish"     
              @before-upload="beforeUpload"
              :show-file-list="false"
              >
              <img style="width: 60%; height: 80%; margin-top: 6px; margin-left: 8px;" src="/src/assets/upload.png"/>
            </n-upload>
          
          </HoverButton>
    
          <NAutoComplete v-model:value="prompt" :options="searchOptions" :render-label="renderOption">
            <template #default="{ handleInput, handleBlur, handleFocus }">
              <NInput ref="inputRef" v-model:value="prompt" type="textarea" :placeholder="placeholder"
                :autosize="{ minRows: 1, maxRows: isMobile ? 4 : 8 }" @input="handleInput" @focus="handleFocus"
                @blur="handleBlur" @keypress="handleEnter" />
            </template>
          </NAutoComplete>
          <NButton type="primary" :disabled="buttonDisabled" @click="handleSubmit">
            <template #icon>
              <span class="dark:text-black">
                <SvgIcon icon="ri:send-plane-fill" />
              </span>
            </template>
          </NButton>
        </div>
      </div>
    </footer>
  </div>
  <Setting v-if="showUs" v-model:visible="showUs" />
</template>
